URL = "ftp://ftp.hdfgroup.org/HDF5/prev-releases/hdf5-1.6/hdf5-1.6.10/src/hdf5-%(VERSION)s.tar.gz"
DEPENDS = ["zlib"]
VERSIONS = ["1.6.10"]
DEFAULT_VERSION = "1.6.10"

from source.builders import ConfigureMakeBuilder, VisualStudioBuilder
import os, subprocess, zipfile


class HDF5VisualStudioBuilder( VisualStudioBuilder ):
  """ subclass of ConfigureMakeBuilder with custom configure step """
   
  def configure(self):
    os.chdir( self.extractedpath+"\\windows" )
    zp = zipfile.ZipFile("all.zip")
    zp.extractall( self.extractedpath+"\\windows" )
    
    # Create a new environment
    self.hdf5env = os.environ
    self.hdf5env['HDF5_EXT_ZLIB'] = "zlib.lib"

    # Set the INCLUDE and LIB environment variables
    
    self.hdf5env["INCLUDE"] = self.hdf5env["INCLUDE"]+";"+self.dependencies["zlib"].getincludedir()
    self.hdf5env["LIB"] = self.hdf5env["LIB"]+";"+self.dependencies["zlib"].getlibdir()
    
  def make(self):
    #upgrade
    os.chdir( self.extractedpath+"\\windows\\proj\\all" )    
    self.execute( "devenv all.sln /Upgrade" )

    os.chdir( self.extractedpath+"\\windows\\misc\\typegen" )
    self.execute( "devenv typegen.sln  /Upgrade " )

    #copy to correct location
    self.copydir( self.extractedpath+"\\windows\\misc", self.extractedpath+"\\misc")
    self.copydir( self.extractedpath+"\\windows\\proj", self.extractedpath+"\\proj")

    #get rid of HAVE_SZLIB
    replacements = []
    replacements.append(( ".*define H5_HAVE_SZLIB_H 1", "//define H5_HAVE_SZLIB_H 1" ))
    replacements.append(( ".*define H5_HAVE_FILTER_SZIP 1", "//define H5_HAVE_FILTER_SZIP 1" ))
    replacements.append(( ".*define H5_SZIP_CAN_ENCODE 1", "//define H5_SZIP_CAN_ENCODE 1" ))
    self.replaceLinesInFile( self.extractedpath+"\\windows\\src\\h5pubconf.h", replacements )
    self.copyfile( self.extractedpath+"\\windows\\src\\h5pubconf.h", self.extractedpath+"\\src")    

    #build the init module
    os.chdir( self.extractedpath+"\\misc\\typegen\\h5tinit" )
    self.execute( "devenv h5tinit.vcproj  /Build \"Release\" " )
    self.execute( self.extractedpath+"\\misc\\typegen\\h5tinit\\Release\\h5tinit.exe > "+ self.extractedpath+"\\src\\h5tinit.c" )

    os.chdir( self.extractedpath+"\\proj" )
    
    if self.machine.bits == 64:
      self.convertProjTo64("hdf5\\hdf5.vcproj")
      self.convertProjTo64("hdf5dll\\hdf5dll.vcproj")
      self.convertProjTo64("hdf5_cpp\\hdf5_cpp.vcproj")
      self.convertProjTo64("hdf5_cppdll\\hdf5_cppdll.vcproj")
      self.convertSolutionTo64("all\\all.sln")

    #call devenv using the custom environment for the (additional) include and the lib directories
    #self.debug= True
    #self.call_devenv("all\\all.sln", "hdf5_cppdll", self.hdf5env)
    #self.debug= False
    #self.call_devenv("all\\all.sln", "hdf5_cppdll", self.hdf5env)

    subprocess.call(["devenv.com", "all\\all.sln", "/Project", "hdf5_cpp", "/Build", "Release", "/useenv"], env=self.hdf5env )
    subprocess.call(["devenv.com", "all\\all.sln", "/Project", "hdf5_cpp", "/Build", "Debug", "/useenv"], env=self.hdf5env )   
    subprocess.call(["devenv.com", "all\\all.sln", "/Project", "hdf5_cppdll", "/Build", "Release", "/useenv"], env=self.hdf5env )
    subprocess.call(["devenv.com", "all\\all.sln", "/Project", "hdf5_cppdll", "/Build", "Debug", "/useenv"], env=self.hdf5env )   

  def install(self):
    os.chdir(self.extractedpath)
    bindir = self.installpath() + "/bin"
    libdir = self.installpath() + "/lib"
    includedir = self.installpath() + "/include"

    #dll binaries
    self.copyfile( "proj/hdf5dll/Debug/hdf5ddll.dll", bindir )
    self.copyfile( "proj/hdf5dll/Release/hdf5dll.dll", bindir )
    #dynamic link libs
    self.copyfile( "proj/hdf5dll/Debug/hdf5ddll.lib", libdir )
    self.copyfile( "proj/hdf5dll/Release/hdf5dll.lib", libdir )
    #statics
    self.copyfile( "proj/hdf5/Debug/hdf5d.lib", libdir )
    self.copyfile( "proj/hdf5/Release/hdf5.lib", libdir )
    #headers
    self.copypattern("src", "^.*\\.h$", includedir )

############

def decider( machine, dirs ):
  return { "Linux" : ConfigureMakeBuilder, "Windows" : HDF5VisualStudioBuilder }[machine.platform]

BUILDER = decider

